<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>字典项配置</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!-- 工具栏模板 -->
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

        <!-- 数据表格 -->
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <!-- 行操作按钮模板 -->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

        <!-- 添加窗口 -->
        <div id="add" style="display: none; padding: 20px;">
            <form class="layui-form" lay-filter="addForm">
                <input type="hidden" name="dictId" id="dictIdInput">
                <div class="layui-form-item">
                    <label class="layui-form-label required">字典项文本</label>
                    <div class="layui-input-block">
                        <input type="text" name="itemText" lay-verify="required" placeholder="请输入字典项文本" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">字典项值</label>
                    <div class="layui-input-block">
                        <input type="text" name="itemValue" lay-verify="required" placeholder="请输入字典项值" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">附值</label>
                    <div class="layui-input-block">
                        <input type="text" name="itemAttachValue" placeholder="请输入附值（可选）" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">描述</label>
                    <div class="layui-input-block">
                        <input type="text" name="description" placeholder="请输入描述" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input type="number" name="sortOrder" placeholder="请输入排序号" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="1" title="启用" checked>
                        <input type="radio" name="status" value="0" title="禁用">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="saveAddBtn">保存</button>
                    </div>
                </div>
            </form>
        </div>

        <!-- 编辑窗口 -->
        <div id="edit" style="display: none; padding: 20px;">
            <form class="layui-form" lay-filter="editForm">
                <input type="hidden" name="id" id="editId">
                <input type="hidden" name="dictId" id="editDictId">
                <div class="layui-form-item">
                    <label class="layui-form-label required">字典项文本</label>
                    <div class="layui-input-block">
                        <input type="text" name="itemText" lay-verify="required" placeholder="请输入字典项文本" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">字典项值</label>
                    <div class="layui-input-block">
                        <input type="text" name="itemValue" lay-verify="required" placeholder="请输入字典项值" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">附值</label>
                    <div class="layui-input-block">
                        <input type="text" name="itemAttachValue" placeholder="请输入附值（可选）" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">描述</label>
                    <div class="layui-input-block">
                        <input type="text" name="description" placeholder="请输入描述" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input type="number" name="sortOrder" placeholder="请输入排序号" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="1" title="启用">
                        <input type="radio" name="status" value="0" title="禁用">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="saveEditBtn">保存</button>
                    </div>
                </div>
            </form>
        </div>

    </div>
</div>

<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer', 'jquery'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;

        // 获取 URL 参数中的 dictId
        const urlParams = new URLSearchParams(window.location.search);
        const dictId = urlParams.get('id');

        if (!dictId) {
            layer.alert("缺少字典ID参数，请从字典管理页面进入", { icon: 2 }, function () {
                window.close();
            });
            return;
        }

        // 设置隐藏字段
        $('#dictIdInput').val(dictId);

        table.render({
            elem: '#currentTableId',
            url: '/sysDictItem/queryPage?dictId=' + dictId,
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.list
                };
            },
            toolbar: '#toolbarDemo',  // 设置工具栏模板
            cols: [[
                { type: "checkbox", width: 50 },
                { field: 'id', width: 80, title: 'ID' },
                { field: 'itemText', width: 150, title: '字典项文本' },
                { field: 'itemValue', width: 150, title: '字典项值' },
                { field: 'itemAttachValue', width: 150, title: '附值' },
                { field: 'description', width: 200, title: '描述' },
                { field: 'sortOrder', width: 100, title: '排序' },
                { field: 'status', width: 100, title: '状态', templet: function(d) {
                        return d.status === 1 ? '启用' : '禁用';
                    }},
                { title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center" }  // 行操作按钮模板
            ]],
            limits: [5, 10, 20, 50],
            limit: 10,
            page: true,
            skin: 'line'
        });




        // 添加按钮
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {
                form.val("addForm", {});
                $('#dictIdInput').val(dictId);
                layer.open({
                    title: '添加字典项',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['75%', '75%'],
                    content: $('#add')
                });
            }
        });

        // 行操作
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                form.val("editForm", data);
                $('#editDictId').val(dictId);
                layer.open({
                    title: '编辑字典项',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['75%', '75%'],
                    content: $('#edit')
                });
            } else if (obj.event === 'delete') {
                $.getJSON('/sysDictItem/delete?id=' + data.id, function (res) {
                    if (res.code === 0) {
                        layer.msg("删除成功");
                        obj.del();
                    } else {
                        layer.msg("删除失败");
                    }
                });
            }
        });

        // 保存添加
        form.on('submit(saveAddBtn)', function (data) {
            $.post("/sysDictItem/add", data.field, function (res) {
                if (res.code === 0) {
                    layer.msg("添加成功");
                    table.reload('currentTableId');
                } else {
                    layer.msg("添加失败");
                }
                layer.closeAll();
            }, "json");
            return false;
        });

        // 保存修改
        form.on('submit(saveEditBtn)', function (data) {
            $.post("/sysDictItem/update", data.field, function (res) {
                if (res.code === 0) {
                    layer.msg("修改成功");
                    table.reload('currentTableId');
                } else {
                    layer.msg("修改失败");
                }
                layer.closeAll();
            }, "json");
            return false;
        });

    });
</script>
</body>
</html>
